
<?php
//模块插件类
//对外接口函数类，其他插件可通过“$this->plugin('jy_juhesms')->方法名” 来调用本类的方法
defined('BY_JYA') or exit('error');
class func_jy_toutiao extends plugin{
	public function new_article($title,$content,$abstract='',$save=1,$article_tag='news_society'){
		$setting=$this->get_setting();
		$access_token=$setting['access_token'];
		$client_key=$setting['client_key'];
		if(!$access_token){
			exi('access_token未填写','error');
		}
		if(!$client_key){
			exi('client_key未填写','error');
		}
		$url = 'https://mp.toutiao.com/open/new_article_post/?access_token='.$access_token.'&client_key='.$client_key; //发布图文接口的URL
		$post=array('title'=>$title,'content'=>$content,'abstract'=>$abstract,'save'=>$save,'article_tag'=>$article_tag);
		$post=ijson_encode($post);
		$res = cfc('http')->ihttp_request($url,$post);
		$shuju=json_decode($res['content'],true);
		$message=$shuju['message'];
		$data=$shuju['data'];
		if($message=='error'){
			exi('发布头条失败！'.$data,'error');
		}
		return $message;
	}
	public function video($type,$path){//返回值  upload_id 
		$setting=$this->get_setting();
		$access_token=$setting['access_token'];
		$client_key=$setting['client_key'];
		if(!$access_token){
			exi('access_token未填写','error');
		}
		if(!$client_key){
			exi('client_key未填写','error');
		}
		//获取视频上传地址
		if($type== 1){
			$url='https://mp.toutiao.com/open/video/get_upload_url/?access_token='.$access_token.'&client_key='.$client_key; //整体上传
		}
		else{
			$url='https://mp.toutiao.com/open/video/get_chunk_upload_info/?access_token='.$access_token.'&client_key='.$client_key; //分片上传
		}
		$data = cfc('http')->ihttp_request($url);
		
		//获取视频文件内容
		$video_file = fopen($path, "rb");
		$video_data = fread($video_file);
		fclose($video_file);
		$file_size =len($video_data); // 视频文件总长度
		$chunk_size = 1 * 1024 * 1024; // 每个分片大小，1M（可根据需要自定义分片大小）
		$i = 0;
		$start = 0; //分片开始位置
		$end = 0;//分片结束位置
		// 分片上传
		while ($end < $file_size){
			$start = $i * $chunk_size;
			$end = $start + $chunk_size;
			if($end>$file_size){
				$end = $file_size;
				//$payload = array('video_file'=>$video_data[$start:$end]);
				//$headers = {'Content-Range': 'bytes %s-%s/%s' % ($start, $end-1, $file_size)}; # header中必须有Content-Range
				//$url=$url.'&files='.$payload.'&headers='.$headers;
				$send_video = cfc('http')->ihttp_request($url);
				if (200 != $send_video['status_code']){
					return $send_video;
				}
			}
			$i = $i + 1;
		}
	}
	//获取视频详细信息
	public function get_video($upload_id){
		$setting=$this->get_setting();
		$access_token=$setting['access_token'];
		$client_key=$setting['client_key'];
		$url='https://mp.toutiao.com/open/video/get_video_meta_info/?access_token='.$access_token.'&client_key='.$client_key.'&upload_id='.$upload_id;
		$data=cfc('http')->ihttp_request($url);
		$status_desc=$data['status_desc'];//描述当前视频状态信息
		$status=$data['status'];//描述当前视频状态
		return $data;
	}
	//删除视频
	public function delete_video($upload_id){
		$setting=$this->get_setting();
		$access_token=$setting['access_token'];
		$client_key=$setting['client_key'];
		$url='https://mp.toutiao.com/open/video/delete_video/?access_token='.$access_token.'&client_key='.$client_key.'&upload_id='.$upload_id;
		$data=cfc('http')->ihttp_request($url);
		$code=$data['code'];//	0表示成功，非0表示失败
		return $data;
	}
	

}
?>
